﻿' _______                 _      _        _____               _       
'|__   __|               | |    | |      |  __ \             | |      
'   | | _   _  _ __ ___  | |__  | |  ___ | |  | |  ___   ___ | | __   
'   | || | | || '_ ` _ \ | '_ \ | | / _ \| |  | | / _ \ / __|| |/ /   
'   | || |_| || | | | | || |_) || ||  __/| |__| ||  __/| (__ |   <  _ 
'   |_| \__,_||_| |_| |_||_.__/ |_| \___||_____/  \___| \___||_|\_\(_)
Imports TumblePower

Public Class TumbleDeck

    Private TumblrData As tumblr
    Dim acct As New tumblr.Account
    Dim textpostform As New TextPost(acct)
    Dim photopostform As New PhotoPost(acct)
    Dim quotepostform As New QuotePost(acct)
    Dim linkpostform As New LinkPost(acct)
    Dim chatpostform As New ChatPost(acct)
    Dim audiopostform As New AudioPost(acct)
    Dim videopostform As New VideoPost(acct)
    Dim tumblarityform As New Tumblarity
    Dim preferencesform As New Preferences

    Sub LogIn()
        Me.Cursor = Cursors.WaitCursor
        LogInButton.Enabled = False
        Dim account As New tumblr.Account
        TumblrData = New tumblr()
        account.Email = EmailTextBox.Text
        account.Password = PasswordTextBox.Text
        If (account.Email.Length > 0) AndAlso (account.Password.Length > 0) Then
            Dim aA As New tumblr.aAuthenticate(AddressOf TumblrData.Authenticate)

            Dim result As IAsyncResult = aA.BeginInvoke(account, New AsyncCallback(AddressOf AuthCallbackMethod), aA)
        Else
            LogInButton.Enabled = True
            Me.Cursor = Cursors.[Default]
            MessageBox.Show(Me, "Please check your email and/or password.", "Oops! Something went wrong.", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End If
    End Sub

    Private Sub LogInButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogInButton.Click
        LogIn()
    End Sub

    Private Sub AuthCallbackMethod(ByVal ar As IAsyncResult)
        If Me.InvokeRequired Then
            Me.BeginInvoke(New AsyncCallback(AddressOf AuthCallbackMethod), ar)
            Exit Sub
        End If
        Dim caller As tumblr.aAuthenticate = DirectCast(ar.AsyncState, tumblr.aAuthenticate)
        Dim stat As Status = caller.EndInvoke(ar)
        LogInButton.Enabled = True
        Me.Cursor = Cursors.[Default]
        If stat.Code = Status.KnownCodes.Ok Then
            My.Settings.Email = EmailTextBox.Text
            My.Settings.Password = PasswordTextBox.Text
            acct.Email = EmailTextBox.Text
            acct.Password = PasswordTextBox.Text
            ToolStripMenuItem2.Enabled = True
            CreateNewToolStripMenuItem.Enabled = True
            MyDashboardButton.Select()
        Else
            MsgBox(stat.Msg)
        End If
    End Sub

    Private Sub TumbleDeck_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CreateNewToolStripMenuItem.Enabled = False
        ToolStripMenuItem2.Enabled = False
        If My.Settings.Email <> "" Then EmailTextBox.Text = My.Settings.Email
        If My.Settings.Password <> "" Then PasswordTextBox.Text = My.Settings.Password
        'AutomaticLogIn - use "LogIn()" to log in
        If My.Settings.AutomaticLogIn = True And EmailTextBox.Text = "" = False And PasswordTextBox.Text = "" = False Then
            LogIn()
        End If
        If My.Settings.AlwaysOnTop = True Then
            Me.TopMost = True
        Else
            Me.TopMost = False
        End If
    End Sub

    Private Sub OnTopForNowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OnTopForNowToolStripMenuItem.Click
        If OnTopForNowToolStripMenuItem.Checked = False Then
            Me.TopMost = False
        Else
            Me.TopMost = True
        End If
    End Sub

    Private Sub MyDashboardButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyDashboardButton.Click
        Process.Start("http://tumblr.com/dashboard")
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        MsgBox("TumbleDeck" & vbLf & "Copyright © 2011 The TumbleDeck Project" & vbLf & "All rights reserved." & vbLf & vbLf & "This application is open-source, is written under the New BSD License (BSD) and shall NOT be sold." & vbLf & vbLf & "Head Developer: Rafal M. Chmiel" & vbLf & "Senior Developer: Deidre Yarlett" & vbLf & vbLf & "Logo by Jojo Mendoza (http://deleket.com/)" & vbLf & "Icons by Yusuke Kamiyamane (http://p.yusukekamiyamane.com/)", MsgBoxStyle.Information)
    End Sub

    Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click
        Process.Start("http://tumbledeck.codeplex.com/documentation")
    End Sub

    Private Sub ForgotYourPasswordLinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles ForgotYourPasswordLinkLabel.LinkClicked
        Process.Start("http://www.tumblr.com/forgot_password")
    End Sub

    Private Sub TextPostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextPostToolStripMenuItem.Click
        textpostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
        textpostform.ShowDialog()
    End Sub

    Private Sub QuotePostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuotePostToolStripMenuItem.Click
        quotepostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem6.Click
        quotepostform.ShowDialog()
    End Sub

    Private Sub LinkPostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkPostToolStripMenuItem.Click
        linkpostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem7.Click
        linkpostform.ShowDialog()
    End Sub

    Private Sub ChatPostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChatPostToolStripMenuItem.Click
        chatpostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem8.Click
        chatpostform.ShowDialog()
    End Sub

    Private Sub PhotoPostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PhotoPostToolStripMenuItem.Click
        photopostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem5.Click
        photopostform.ShowDialog()
    End Sub

    Private Sub VideoPostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VideoPostToolStripMenuItem.Click
        videopostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem10.Click
        videopostform.ShowDialog()
    End Sub

    Private Sub TumbleDeckNotifyIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TumbleDeckNotifyIcon.MouseDoubleClick
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Show()
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub

    Private Sub TumbleDeck_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If My.Settings.MinimizeToTray = True Then
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Hide()
            End If
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub ExitToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem1.Click
        Application.Exit()
    End Sub

    Private Sub TumblarityToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TumblarityToolStripMenuItem.Click
        tumblarityform.ShowDialog()
    End Sub

    Private Sub PreferencesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreferencesToolStripMenuItem.Click
        preferencesform.ShowDialog()
    End Sub

    Private Sub AudioPostToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AudioPostToolStripMenuItem.Click
        audiopostform.ShowDialog()
    End Sub

    Private Sub ToolStripMenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem9.Click
        audiopostform.ShowDialog()
    End Sub
End Class